

*************************
****  Histograms ********
*************************

capt drop _est_*


*********    LAW *************
eststo drop *

preserve
keep if SampleMain==1 

gen h1=law_average
bysort id_eez_tax: egen helpvar=mean(h1)

summarize helpvar , det
gen help1 =r(mean)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,3)
levelsof help3 ,  local(lMeanVar)  clean
dis `lMeanVar'
drop help1 help2 help3

summarize helpvar , det
gen help1 =r(p50)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,3)
levelsof help3 ,  local(lMedian)  clean
dis `lMedian'
drop help1 help2 help3

summarize helpvar , det
gen help1 =r(p90)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,4)
levelsof help3 ,  local(ltop)  clean
dis `ltop'


*** Histogram   ***
twoway ///
(histogram helpvar if $TreatmentGroup==1 & SampleMain==1   , ///
lcolor(red*0.7) color(red*0.7) lpattern(solid)  start(-2) width(0.1)  percent) ///
(histogram helpvar if $TreatmentGroup==0 & SampleMain==1  , ///
lcolor(black) color(none) lpattern(solid)  start(-2) width(0.1)   percent) ///
,    ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ) ///
legend(off) ///
text(28 -2 "Mean:`lMeanVar' ", place(e)) ///
text(25 -2 "Median:`lMedian' ", place(e)) ///
text(22 -2 "90th:`ltop' ", place(e)) ///
scale(1.9)  xtitle("")  
graph export "${figures}Hist_RoL_TC.${fileformat}", as(${fileformat}) replace
restore



*********    K    *************
eststo drop *
preserve
keep if SampleMain==1 

drop if K>2
gen h1=K
bysort id_eez_tax: egen helpvar=mean(h1)
summarize helpvar 

gen help1 =r(mean)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,3)
levelsof help3 ,  local(lMeanVar)  clean
dis `lMeanVar'
drop help1 help2 help3

summarize helpvar , det
gen help1 =r(p50)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,3)
levelsof help3 ,  local(lMedian)  clean
dis `lMedian'
drop help1 help2 help3


*** Histogram   ***
twoway ///
(histogram helpvar if $TreatmentGroup==1 & SampleMain==1   , ///
lcolor(red*0.7) color(red*0.7) lpattern(solid)  start(0)  width(0.1)  percent) ///
(histogram helpvar if $TreatmentGroup==0 & SampleMain==1  , ///
lcolor(black) color(none) lpattern(solid) lwidth(medthin) width(0.1)  start(0)   percent) ///
,    ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ) ///
legend(off) ///
text(35 1.2 "Mean:`lMeanVar' ", place(e)) ///
text(31 1.2 "Median:`lMedian' ", place(e)) ///
scale(1.9)  xtitle("")  
graph export "${figures}Hist_K_TC.${fileformat}", as(${fileformat}) replace
restore



*********    Trade    *************
eststo drop *
preserve
keep if SampleMain==1 
gen h1=trade
bysort id_eez_tax: egen helpvar=mean(h1)
drop if helpvar>300
summarize helpvar 

gen meanR=round(r(mean))
local lMeanVar=meanR


summarize helpvar , det
gen help1 =r(p50)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,2)
levelsof help3 ,  local(lMedian)  clean
dis `lMedian'
drop help1 help2 help3

*** Histogram  ***
twoway ///
(histogram helpvar if $TreatmentGroup==1 & SampleMain==1   , ///
lcolor(red*0.7) color(red*0.7) lpattern(solid)  start(0) width(10)  percent) ///
(histogram helpvar if $TreatmentGroup==0 & SampleMain==1  , ///
lcolor(black) color(none) lpattern(solid)  start(0)  width(10) percent) ///
,    ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ) ///
legend(off) ///
text(35 200 "Mean:`lMeanVar'", place(e)) ///
text(31 200 "Median:`lMedian'", place(e)) ///
scale(1.9)  xtitle("")  
graph export "${figures}Hist_Trade_TC.${fileformat}", as(${fileformat}) replace
restore



*********  Histogram: Species price    *************

eststo drop *

preserve

bysort taxonkey: egen hvar=mean(realprice_taxon) if iyear>=$PriceYearStart & iyear<=$PriceYearEnd
bysort taxonkey: egen helpvar=mean(hvar)

sum helpvar, det
dis `r(p1)'  
dis `r(p99)'
drop if helpvar <= `r(p1)'  | helpvar >= `r(p99)'
*drop if helpvar >= `r(p99)'

sum helpvar, det
gen help1 =r(mean)
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,4)
levelsof help3 ,  local(lMeanVar)  clean
dis `lMeanVar'
drop help1 help2 help3

sum helpvar, det
gen help1 =r(p50)
dis `r(p50)'
levelsof help1 ,  local(lhelp1) 
gen help2="`lhelp1'"
gen help3=substr(help2,1,4)
levelsof help3 ,  local(lMedianVar)  clean
dis `lMedianVar'


*** Histogram T & C   ***
twoway ///
(histogram helpvar if $TreatmentGroup==1   , ///
lcolor(red*0.7) color(red*0.7) lpattern(solid)  start(0)  width(600)  percent) ///
(histogram helpvar if $TreatmentGroup==0   , ///
lcolor(black) color(none) lpattern(solid) lwidth(medthin) width(600)  start(0)   percent) ///
,    ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ) ///
legend(off) ///
text(19 6000 "Mean:`lMeanVar' ", place(e)) ///
text(16 6000 "Median:`lMedianVar' ", place(e)) ///
scale(1.9)  xtitle("")  
graph export "${figures}Hist_Price_TC.${fileformat}", as(${fileformat}) replace
restore



********* Event:  Collapsed - Long controls. 2 year averages  *************
eststo drop *

preserve
gen h1=law_average
bysort id_eez_tax: egen helpvar=mean(h1)
xtile percentile = helpvar, nq(10)
keep if percentile<=9  /*keep if percentile<9*/

xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo 
esttab *, keep(*impl*)


forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****


/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results
*/

xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("") ///
ytitle(Effect of PPR) ///
ylab( -0.8(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}\Event_Collapse_2y_ControlsLong_RoL_Low.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************


********* Event:  Collapsed - Long controls. 2 year averages  *************
eststo drop *

preserve
gen h1=law_average
bysort id_eez_tax: egen helpvar=mean(h1)
xtile percentile = helpvar, nq(10)
keep if percentile>9
sum helpvar if percentile>9

xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo 
esttab *, keep(*impl*)


forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****


/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results
*/

xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("") ytitle(Effect of PPR) ///
ylab( -0.8(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}\Event_Collapse_2y_ControlsLong_RoL_High.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************





********* Event:  Collapsed - Long controls. 2 year averages  *************
eststo drop *

preserve
gen h1=trade
bysort id_eez_tax: egen helpvar=mean(h1)
drop if helpvar>300

xtile percentile = helpvar, nq(10)
keep if percentile<=5   /*keep if percentile<5*/

xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo 
esttab *, keep(*impl*)


forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****


/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results
*/

xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("") ytitle(Effect of PPR) ///
ylab( -0.8(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}\Event_Collapse_2y_ControlsLong_Trade_Low.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************


********* Event:  Collapsed - Long controls. 2 year averages  *************
eststo drop *

preserve
gen h1=trade
bysort id_eez_tax: egen helpvar=mean(h1)
drop if helpvar>300

xtile percentile = helpvar, nq(10)
keep if percentile>5

xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo 
esttab *, keep(*impl*)


forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results
*/

xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("") ytitle(Effect of PPR) ///
ylab( -0.8(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}\Event_Collapse_2y_ControlsLong_Trade_High.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************


********* Event:  Collapsed - Long controls. 2 year averages  *************
eststo drop *

preserve
gen helpvar=K
sum helpvar , det
dis `r(p99)'
drop if helpvar>`r(p99)'

xtile percentile = helpvar, nq(2)
tab percentile
sum helpvar if percentile<=1  	/*Below MEDIAN*/
sum helpvar if percentile>=2	/*Above MEDIAN*/
keep if percentile<=1 			/*Below median*/
/*
sum helpvar , det
dis `r(mean)'
keep if helpvar>`r(mean)'
*/

/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($ClusterLevel) 
eststo 
esttab *, keep(*impl*)
*/

forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****


xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($ClusterLevel) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("Years relative to implementation") ytitle(Effect of PPR) ///
ylab( -0.6(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}\Event_Collapse_2y_ControlsLong_K_Low.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************


********* Event:  Collapsed - Long controls. 2 year averages  *************
eststo drop *

preserve
gen helpvar=K
sum helpvar , det
dis `r(p99)'
drop if helpvar>`r(p99)'

xtile percentile = helpvar, nq(2)
tab percentile
sum helpvar if percentile<=1  	/*Below MEDIAN*/
sum helpvar if percentile>=2	/*Above MEDIAN*/
keep if percentile>=2 /*Above median*/
/*
sum helpvar , det
dis `r(mean)'
keep if helpvar>`r(mean)'
*/

/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($Clusterlevel) 
eststo 
esttab *, keep(*impl*)
*/

forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****


xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($Clusterlevel) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("Years relative to implementation") ytitle(Effect of PPR) ///
ylab( -0.6(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}\Event_Collapse_2y_ControlsLong_K_High.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************



********* Event:  Collapsed - Long controls. 2 year averages  *************
*****  Species price *****
**** High value / price ****
eststo drop *
preserve

bysort taxonkey: egen hvar=mean(realprice_taxon) if iyear>=$PriceYearStart & iyear<=$PriceYearEnd
bysort taxonkey: egen helpvar=mean(hvar)

sum helpvar, det
dis `r(p1)'  
dis `r(p99)'
drop if helpvar>=`r(p99)' | helpvar<=`r(p1)'
*drop if helpvar<=`r(p1)'

xtile percentile = helpvar, nq(2)
tab percentile
sum helpvar if percentile==1  	/*Below MEDIAN*/
sum helpvar if percentile==2	/*Above MEDIAN*/
keep if percentile==2 /*Above median*/

/*
sum helpvar , det
dis `r(mean)'
keep if helpvar>`r(mean)'
*/

/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($ClusterLevel) 
eststo 
esttab *, keep(*impl*)
*/

forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****



xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($ClusterLevel) 
eststo results


xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster(id_eez_tax) 
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("") ///
ytitle(Effect of PPR) ///
ylab( -0.6(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}Event_Collapse_2y_ControlsLong_Price_High.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************


********* Event:  Collapsed - Long controls. 2 year averages  *************
**** Low value / price ****
eststo drop *

preserve

bysort taxonkey: egen hvar=mean(realprice_taxon) if iyear>=$PriceYearStart & iyear<=$PriceYearEnd
bysort taxonkey: egen helpvar=mean(hvar)

sum helpvar, det
dis `r(p1)'  
dis `r(p99)'
drop if helpvar>=`r(p99)' | helpvar<=`r(p1)'
*drop if helpvar<=`r(p1)'

xtile percentile = helpvar, nq(2)
tab percentile
sum helpvar if percentile<=1  	/*Below MEDIAN*/
sum helpvar if percentile>=2	/*Above MEDIAN*/
keep if percentile<=1 			/*Below median*/


/*
xtset id_eez_tax iyear
reghdfe collapsed $Treatment  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )  cluster($Clusterlevel) 
eststo 
esttab *, keep(*impl*)
*/

forvalues i=60(-1)1 {
gen TimeRelPPR_m`i'=0
replace TimeRelPPR_m`i'=1 if policy_time_index==-`i'
}

forvalues i=1(1)60 {
gen TimeRelPPR_p`i'=0
replace TimeRelPPR_p`i'=1 if policy_time_index==`i'
}
***

*** Minus: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_ym`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_ym`j'=1 if TimeRelPPR_m`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****

*** Positive: 2 year averages
forvalues i=2(2)60 {
gen TimeRelPPR_yp`i'=0
}
local min = 1
local max = 2
forvalues j=2(2)60 {
forvalues i=`min'(1)`max' {
replace TimeRelPPR_yp`j'=1 if TimeRelPPR_p`i'==1
}
local j = `j' + 2
local min = `min' +2
local max = `max' +2
}
****


xtset id_eez_tax iyear
reghdfe collapsed TimeRelPPR_ym*  TimeRelPPR_yp*  ///
$ControlsLong ///
, absorb( i.id_eez_tax i.iyear )   cluster($Clusterlevel)
eststo results

parmest, label list(parm label estimate min* max* p) saving(results, replace)

use results, clear
gen stuff_present =  strpos(parm, "TimeRelPPR_") > 0
drop if stuff_present==0
drop dof  label
gen helpvar=_n
gen length=length(parm)
gen time_str = substr(parm,14,1)  if length==14 
replace time_str = substr(parm,14,2)  if length==15
drop if p==.
destring time_str , generate(time_num)

gen minus=0
replace minus=1 if strpos(parm, "ym")
replace time_num=time_num*-1 if minus==1 
sort time_num

label variable estimate "DiD estimate"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"

sum time_num
summarize helpvar
local max=r(max)
expand 2 if helpvar==`max'
gen helpvar2=_n
summarize helpvar2
local max=r(max)
replace estimate=0 if helpvar2==`max'
replace min95=0 if helpvar2==`max'
replace max95=0 if helpvar2==`max'
replace time_num=0 if helpvar2==`max'

drop if  time_num<-$TimeMin | time_num>$TimeMax

twoway ///
(rarea min95 max95 time_num , sort color(gs13)) ///
(connected  estimate time_num , sort mcolor(blue) lpattern(solid) lcolor(blue)   msymbol(o)     ) ///
, yline(0) xline(0 , lpattern(dash) ) ///
scheme(s2mono) graphregion(fcolor(white) lcolor(white)ifcolor(white) ///
ilcolor(white))  legend(off) ///
xtitle("") ///
ytitle(Effect of PPR) ///
ylab( -0.6(0.2)0.4  )  xlabel(-$TimeMin (10) $TimeMax )  scale(1.9)
graph export "${figures}Event_Collapse_2y_ControlsLong_Price_Low.${fileformat}", as(${fileformat}) /*preview(on)*/ replace
restore
**************


capt drop _est_*

